{template 'web/'.$cfg['floder'].'/base_menu'}
<div class="top_tab"> 
  <a href="{php echo $this->createWebUrl('sale')}" class="layui-btn {if $operation != 'display'}layui-btn-primary{/if}">销售报表</a>
    {if pdo_fetchcolumn("SELECT count(*) FROM ".tablename('j_hmoney_elemtoken')." WHERE groupid=:a ",array(':a'=>$shopid))}
    <a href="{php echo $this->createWebUrl('elem')}" class="layui-btn layui-btn-primary">饿了么-汇总</a>
    {/if}
    <a href="{php echo $this->createWebUrl('sale',array('op'=>'charge'))}" class="layui-btn {if $operation != 'charge'}layui-btn-primary{/if}">充值报表</a>
  <div class="secrchinput" style="display: none">
    <button type="button" onclick="getpage(1)"><i class="fa fa-search"></i></button>
    <input type="text" id="txtTopupSeachStr" placeholder="输入单号">
  </div>
</div>
<style>
.panelbox-head a{ margin-right:10px}
.green{ color:#090}
.orange{ color:#F60}
.showboxcatainer{display:-moz-box; /* Firefox */  display:-webkit-box; /* Safari and Chrome */  display:box;}
.showbox{-moz-box-flex:2; /* Firefox */  -webkit-box-flex:2; /* Safari and Chrome */box-flex:2;}
.showboxtxt{ margin-right: 10px;  background:#EEE; padding:10px}
.showboxtxt h3{ font-size:12px; text-align:left; line-height:24px;}
.showboxtxt h3 b{ font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#F60}
h2{font-size:18px; margin:10px;}
</style>
{if $operation == 'display'}
<div class="panelbox">
<form action="" method="post" class="layui-form" enctype="multipart/form-data">
  <div class="panelbox-head form-inline">
  	<a href="javascript:output(0)" class="layui-btn layui-btn-small"><i class="layui-icon">&#xe601;</i> 导出订单</a>
    <a href="javascript:output(1)" class="layui-btn layui-btn-small"><i class="layui-icon">&#xe601;</i> 导出商品</a>
    <div class="layui-btn-group selecttime">
      <button type="button" class="layui-btn layui-btn-small">今天</button>
      <button type="button" class="layui-btn layui-btn-primary layui-btn-small">昨天</button>
      <button type="button" class="layui-btn layui-btn-primary layui-btn-small">近7天</button>
      <button type="button" class="layui-btn layui-btn-primary layui-btn-small">其他</button>
    </div>
    <span id="top_time" style="display:none">{php echo tpl_form_field_daterange('gametime', array('start' => date('Y-m-d H:i'),'end'=>date('Y-m-d H:i')),true)}
        <select id="keytype" style="display:inline-block;" lay-ignore class="form-control">
            <option value="" {if $_GPC['keytype']}selected{/if}>搜索范围</option>
            <option value="1" {if $_GPC['keytype']==1}selected{/if}>订单号</option>
            <option value="2" {if $_GPC['keytype']==2}selected{/if}>会员</option>
        </select>
        <input type="text" id="keyword" value="{$_GPC['keyword']}" class="form-control" placeholder="请输入关键字"/>

        <button type="button" onclick="getpage(1)" class="layui-btn layui-btn-danger layui-btn-small">查询</button></span>
    <input type="hidden" name="dateselect" value="1" />
    
  </div>
	<div class="panelbox-body">
		<Div class="showboxcatainer">
            <div class="showbox">
                <div class="showboxtxt">
                    <p>单数</p>
                    <h3><b id="box1_1">0</b></h3>
                </div>
            </div>
			<div class="showbox">
				<div class="showboxtxt">
					<p>总金额</p>
					<h3>￥ <b id="box1_2">0</b> </h3>
				</div>
			</div>
			<div class="showbox">
				<div class="showboxtxt">
					<p>优惠金额</p>
					<h3>￥ <b id="box1_3">0</b> </h3>
				</div>
			</div>
			<div class="showbox">
				<div class="showboxtxt">
					<p>付款金额</p>
					<h3>￥ <b id="box1_4">0</b> </h3>
				</div>
			</div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>退款</p>
                    <h3>￥ <b id="box3_1">0</b> </h3>
                </div>
            </div>
			<div class="showbox">
				<div class="showboxtxt">
					<p>客单价</p>
					<h3>￥ <b id="box1_5">0</b> </h3>
				</div>
			</div>
			<div class="showbox">
				<div class="showboxtxt">
					<p>销售商品总数</p>
					<h3><b id="box1_6">0</b> 件</h3>
				</div>
			</div>
		</Div>
        <Div class="showboxcatainer" style="margin-top: 10px">
            {loop _getPaytpye("",$shopid) $index $row}
            <div class="showbox">
                <div class="showboxtxt">
                    <p>{php echo $row ? $row : "其他"}</p>
                    <h3>笔数：<b id="paybox{$index}_1">0</b> </h3><h3>金额：￥ <b id="paybox{$index}_2">0</b></h3>
                </div>
            </div>
            {/loop}
        </Div>
	</div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <thead>
          <tr>
            <th>单号</th>
            <th>对象</th>
            <th>总金额</th>
            <th>优惠</th>
			<th>实付</th>
            <th>支付方式</th>
            <th>收银员</th>
            <th>时间</th>
            <th>状态</th>
            <th>备注</th>
            <th style="text-align:right">操作</th>
          </tr>
        </thead>
        <tbody id="salelist">
        </tbody>
      </table>
    </div>
  </div>
  <div class="panelbox-foot text-right">  <div id="pagecount"></div></div>
  </form>
</div>
<script language="javascript">
    var shopPayType=eval("("+'{php echo json_encode(_getPaytpye("",$shopid))}'+")");
    var tableName=eval("("+'{php echo json_encode($tableAry)}'+")");
$(".selecttime button").on("click",function(){
	var val=$(this).index()+1;
	$("input[name='dateselect']").val(val);
	$(this).removeClass("layui-btn-primary").siblings().addClass("layui-btn-primary");
	if($(this).text()=="其他"){
		$("#top_time").show();
	}else{
		$("#top_time").hide();
		getpage(1);
	}
});
layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  getpage(1);
  
  form.on('submit(submitbtn)', function(data){
    
    return true;
  });
});
function output(obj){
	var keyword=$("#keyword").val().length>0 ? $("#keyword").val() : '';
	if(obj==0){
		window.open("{php echo $this->createWebUrl('sale',array('op'=>'outputs'))}&keyword="+keyword+"&datetype="+$("input[name='dateselect']").val()+"&statrtime="+$("input[name='gametime[start]']").val()+"&endime="+$("input[name='gametime[end]']").val());
	}else{
		window.open("{php echo $this->createWebUrl('sale',array('op'=>'outputs_detail'))}&keyword="+keyword+"&datetype="+$("input[name='dateselect']").val()+"&statrtime="+$("input[name='gametime[start]']").val()+"&endime="+$("input[name='gametime[end]']").val());
	}
}
function getpage(pageindex){
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	var keyword=$("#keyword").val().length>0 ? $("#keyword").val() : '';
	var searchdata={
		"keyword":keyword,
        "keytype":$("#keytype option:selected").val(),
        "datetype":$("input[name='dateselect']").val(),
		"statrtime":$("input[name='gametime[start]']").val(),	
		"endtime":$("input[name='gametime[end]']").val(),
		"page":pageindex,	
	}
	var userlist=eval("("+'{php echo json_encode($userary)}'+")");
	$.post("{php echo $this->createWebUrl('sale',array('op'=>'salepage'))}",searchdata,function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
			return;
		}
		if(pageindex==1){
			layui.use(['laypage'], function(){
				var laypage = layui.laypage;
				laypage.render({
					elem: 'pagecount'
					,count: result.total
					,limit: 10
					,curr: 1
					,jump: function(obj, first){
						var curr = obj.curr;
						if(first==true)return;
						getpage(curr);
						return;
					}
				});
			});
            $(".showboxcatainer").eq(1).find("b").text("0");

            var sumfee=result.sumfee;
            var refundorder=result.refundorder;
            var refundgoods=result.refundgoods;
            var totalrefundfee=parseInt(refundorder.fee1)+parseInt(refundgoods.fee1);

			$("#box1_1").text(sumfee.num1 ? sumfee.num1 : "0.00");
			$("#box1_2").text(fee2num(sumfee.fee1) ? fee2num(sumfee.fee1) : "0.00");
			$("#box1_3").text(fee2num(sumfee.fee2) ? fee2num(sumfee.fee2) : "0.00");
			$("#box1_4").text(fee2num(sumfee.fee3) ? fee2num(sumfee.fee3) : "0.00");
            var temp1=parseInt(sumfee.num1) ? fee2num(parseInt(parseInt(sumfee.fee3)/parseInt(sumfee.num1))) : "0.00"
			$("#box1_5").text(temp1);
			$("#box1_6").text(sumfee.num2 ? sumfee.num2 : "0.00");
            var paylist=result.paylist;
            for(var i in paylist){
                $("#paybox"+i+"_1").text(paylist[i].num);
                $("#paybox"+i+"_2").text(fee2num(paylist[i].fee1));
            }
            $("#box3_1").text(fee2num(totalrefundfee));
		}
		$("#salelist").empty();
		if(result.success){
			var list=result.list;
			for(var i in list){
				var temp='<tr oid="'+list[i].id+'"><td>'+list[i].serialnum+'<p style="font-size:70%">'+list[i].out_trade_no+'</p>';
				temp+='</td><td>';
                if(!parseInt(list[i].memberno)){
                    temp+='散客';
                }else{
                    temp+=list[i].realname+'<p style="font-size:smaller">'+(list[i].cardno ? list[i].cardno : list[i].wxcardno)+'</p>';
                }
                temp+='</td>';
				temp+='<td>'+fee2num(list[i].order_fee)+'</td><td class="green"> -'+fee2num(list[i].discount_fee)+'</td><td>'+fee2num(list[i].total_fee)+'</td><td>';
				if(parseInt(list[i].ispay)==1){
					if(parseInt(list[i].paytype)==2){
						temp+='<span class="layui-badge layui-bg-orange">现金 '+fee2num(list[i].paid_fee1)+'</span>';
					}else if(parseInt(list[i].paytype)==3){
						temp+='<span class="layui-badge layui-bg-cyan">银行卡'+fee2num(list[i].paid_fee1)+'</span>';
					}else if(parseInt(list[i].paytype)==0){
						temp+='<span class="layui-badge layui-bg-green">微信'+fee2num(list[i].paid_fee1)+'</span>';
					}else if(parseInt(list[i].paytype)==1){
						temp+='<span class="layui-badge layui-bg-blue">支付宝'+fee2num(list[i].paid_fee1)+'</span>';
					}else if(parseInt(list[i].paytype)==4){
						temp+='<span class="layui-badge layui-bg-gray">余额'+fee2num(list[i].paid_fee1)+'</span>';
                    }else{
                        temp+='<span class="layui-badge layui-bg-gray">'+shopPayType[parseInt(list[i].paytype)]+' '+fee2num(list[i].paid_fee1)+'</span>';
					}
					if(parseInt(list[i].paytype2)==2){
						temp+=' <span class="layui-badge layui-bg-orange">现金 '+fee2num(list[i].paid_fee2)+'</span>';
					}else if(parseInt(list[i].paytype2)==3){
						temp+=' <span class="layui-badge layui-bg-cyan">银行卡 '+fee2num(list[i].paid_fee2)+'</span>';
					}else if(parseInt(list[i].paytype2)==0){
						temp+=' <span class="layui-badge layui-bg-green">微信 '+fee2num(list[i].paid_fee2)+'</span>';
					}else if(parseInt(list[i].paytype2)==1){
						temp+=' <span class="layui-badge layui-bg-blue">支付宝 '+fee2num(list[i].paid_fee2)+'</span>';
					}else if(parseInt(list[i].paytype2)==4){
						temp+=' <span class="layui-badge layui-bg-gray">余额 '+fee2num(list[i].paid_fee2)+'</span>';
                    }else if(parseInt(list[i].paytype2)==-1){
                        temp+='';
                    }else{
                        temp+='<span class="layui-badge layui-bg-gray">'+shopPayType[parseInt(list[i].paytype2)]+' '+fee2num(list[i].paid_fee2)+'</span>';
					}
				}
				temp+='</td><td>'+userlist[list[i].userid]+'</td><td>'+fromatTime(list[i].createtime)+'</td><td>';
				
				if(parseInt(list[i].status)==0){
					temp+="<span class='label label-default'>挂单</span>";
				}else if(parseInt(list[i].status)==1){
					temp+="<span class='label label-info'>下单</span>";
				}else if(parseInt(list[i].status)==2){
					temp+="<span class='label label-success'>完成</span>";
				}
				if(parseInt(list[i].ispay)==1){
					if(parseInt(list[i].refundstatus)==1){
						temp+=" <span class='label label-danger'>部分退款</span>";
					}else if(parseInt(list[i].refundstatus)==2){
						temp+=" <span class='label label-danger'>全退款</span>";
					}else{
						temp+=" <span class='label label-success'>已支付</span>";
					}
				}
				temp+='</td><td>'+list[i].remark+'</td><td style="text-align:right"><a href="javascript:getOrderdetail('+list[i].id+')" class="layui-btn layui-btn-small">详</a> <a href="javascript:PrintOrder('+list[i].id+')" class="layui-btn layui-btn-danger layui-btn-small"><i class="fa fa-print"></i></a></td></tr>';
				$("#salelist").append(temp);
			}
		}
	});
}
function getOrderdetail(id){
	if($("#salelist .detailbox").size()>0)$("#salelist .detailbox").remove();
	if($("#salelist tr[oid='"+id+"']").hasClass("isselect"))return;
	$("#salelist tr[oid='"+id+"']").addClass("isselect").siblings().removeClass("isselect");
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createWebUrl('sale',array('op'=>'getorderdetail'))}",{id:id},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
			return;
		}
		if(result.success){
			var list=result.list;
			var temp="";
			for(var i in list){
				temp+='<tr class="detailbox"><td><i class="orange fa fa-menu-right"></i> '+list[i].title+'</td><td></td><td>'+list[i].num+'</td><td>'+fee2num(list[i].total_fee)+'</td><td class="green">';
				if(parseFloat(list[i].price)*100*parseInt(list[i].num)!=parseInt(list[i].total_fee)){
					temp+=fee2num(parseInt(list[i].total_fee)-parseFloat(list[i].price)*100*parseInt(list[i].num));
				}
				temp+='</td><td></td><td></td><td></td><td>';
				if(parseInt(list[i].ispay)){
					temp+='<span class="label label-success">已付</span>';
				}else{
					temp+='<span class="label label-default">未付</span>';
				}
				temp+='</td><td></td><td style="text-align:right">';
				temp+='</td></tr>';
			}
			$(temp).insertAfter($("#salelist tr[oid='"+id+"']"));
			
		}
	});
	
}
</script>
{elseif $operation == 'charge'}
<div class="panelbox">
<form action="" method="post" class="layui-form" enctype="multipart/form-data">
  <div class="panelbox-head">
  	<a href="javascript:output()" class="layui-btn layui-btn-small"><i class="layui-icon">&#xe601;</i> 导出</a>
    <div class="layui-btn-group selecttime">
      <button type="button" class="layui-btn layui-btn-small">今天</button>
      <button type="button" class="layui-btn layui-btn-primary layui-btn-small">昨天</button>
      <button type="button" class="layui-btn layui-btn-primary layui-btn-small">近7天</button>
      <button type="button" class="layui-btn layui-btn-primary layui-btn-small">其他</button>
    </div>
    <span id="top_time" style="display:none">{php echo tpl_form_field_daterange('gametime', array('start' => date('Y-m-d H:i'),'end'=>date('Y-m-d H:i')),true)} <button type="button" onclick="getpage(1)" class="layui-btn layui-btn-danger layui-btn-small">查询</button></span>
    <input type="hidden" name="dateselect" value="1" />
    
  </div>
    <div class="panelbox-body">
        <Div class="showboxcatainer">
            <div class="showbox">
                <div class="showboxtxt">
                    <p>新会员</p>
                    <h3><b id="box2_1">0</b></h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>总会员</p>
                    <h3><b id="box2_2">0</b> </h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>充值金额</p>
                    <h3>￥ <b id="box2_3">0</b></h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>赠送</p>
                    <h3>￥ <b id="box2_4">0</b></h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>消费</p>
                    <h3>￥ <b id="box2_5">0</b></h3>
                </div>
            </div>
        </Div>
        <Div class="showboxcatainer" style="margin-top: 10px">
            <div class="showbox">
                <div class="showboxtxt">
                    <p>微信</p>
                    <h3>金额：￥ <b id="paybox0_1">0</b></h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>支付宝</p>
                    <h3>金额：￥ <b id="paybox1_1">0</b></h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>现金</p>
                    <h3>金额：￥ <b id="paybox2_1">0</b></h3>
                </div>
            </div>
            <div class="showbox">
                <div class="showboxtxt">
                    <p>银行卡</p>
                    <h3>金额：￥ <b id="paybox3_1">0</b></h3>
                </div>
            </div>
        </Div>
    </div>
  <div class="panelbox-body">
    <div class="layui-form">
      <table class="layui-table">
        <thead>
          <tr>
            <th>卡号</th>
            <th>操作类型</th>
            <th>支付类型</th>
            <th>操作人</th>
            <th>操作金额</th>
            <th>赠送金额</th>
            <th>变动后金额</th>
            <th>时间</th>
            <th>状态</th>
            <th>备注</th>
          </tr>
        </thead>
        <tbody id="salelist">
        </tbody>
      </table>
    </div>
  </div>
  <div class="panelbox-foot text-right">  <div id="pagecount"></div></div>
  </form>
</div>
<script language="javascript">
$(".selecttime button").on("click",function(){
	var val=$(this).index()+1;
	$("input[name='dateselect']").val(val);
	$(this).removeClass("layui-btn-primary").siblings().addClass("layui-btn-primary");
	if($(this).text()=="其他"){
		$("#top_time").show();
	}else{
		$("#top_time").hide();
		getpage(1);
	}
});
function output(){
	var keyword=$("#txtTopupSeachStr").val().length>0 ? $("#txtTopupSeachStr").val() : '';
	window.open("{php echo $this->createWebUrl('sale',array('op'=>'outputc'))}&keyword="+keyword+"&datetype="+$("input[name='dateselect']").val()+"&statrtime="+$("input[name='gametime[start]']").val()+"&endtime="+$("input[name='gametime[end]']").val());
}
layui.use(['layer', 'laypage', 'element','form'], function(){
  var layer = layui.layer
  ,laypage = layui.laypage
  ,element = layui.element
  ,form = layui.form;
  getpage(1);
  
  form.on('submit(submitbtn)', function(data){
    
    return true;
  });
});
function getpage(pageindex){
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	var keyword=$("#txtTopupSeachStr").val().length>0 ? $("#txtTopupSeachStr").val() : '';
	var searchdata={
		"keyword":keyword,	
		"datetype":$("input[name='dateselect']").val(),
		"statrtime":$("input[name='gametime[start]']").val(),	
		"endtime":$("input[name='gametime[end]']").val(),
		"page":pageindex,	
	}
	var userlist=eval("("+'{php echo json_encode($userary)}'+")");
	$.post("{php echo $this->createWebUrl('sale',array('op'=>'getcharge'))}",searchdata,function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
			return;
		}
		if(pageindex==1){
			layui.use(['laypage'], function(){
				var laypage = layui.laypage;
				laypage.render({
					elem: 'pagecount'
					,count: result.total
					,limit: 10
					,curr: 1
					,jump: function(obj, first){
						var curr = obj.curr;
						if(first==true)return;
						getpage(curr);
						return;
					}
				});
			});
            var chargeitems=result.chargeitems;
            var chargelist=result.chargelist;
            var chargesale=result.chargesale;
            $("#box2_1").text(result.membercount ? result.membercount : "0");
            $("#box2_2").text(result.memberallcount ? result.memberallcount : "0");
            $("#box2_3").text(chargeitems.fee1 ? fee2num(chargeitems.fee1) : "0.00");
            $("#box2_4").text(chargeitems.fee2 ? fee2num(chargeitems.fee2) : "0.00");
            $("#box2_5").text(chargesale.fee1 ? fee2num(chargesale.fee1) : "0.00");

            $("#paybox0_1").text(chargelist[0]!=null ? fee2num(chargelist[0]) : "0.00");
            $("#paybox1_1").text(chargelist[1]!=null ? fee2num(chargelist[1]) : "0.00");
            $("#paybox2_1").text(chargelist[2]!=null ? fee2num(chargelist[2]) : "0.00");
            $("#paybox3_1").text(chargelist[3]!=null ? fee2num(chargelist[3]) : "0.00");
		}
		$("#salelist").empty();
		if(result.success){
			var list=result.list;
			var temp='';
			for(var i in list){
				temp='<tr><td>'+list[i].cardno+'</td>';
				switch(parseInt(list[i].usetype)){
					case 0:
						temp+='<td><span class="label label-success">充值</span></td>';
					break;
					case 1:
						temp+='<td><span class="label label-warning">消费</span></td>';
					break;
					case 2:
						temp+='<td><span class="label label-danger">退还</span></td>';
					break;
				}
				switch(parseInt(list[i].paytype)){
					case 0:
						temp+='<td><span class="label label-success">微信</span></td>';
					break;
					case 1:
						temp+='<td><span class="label label-info">支付宝</span></td>';
					break;
					case 2:
						temp+='<td><span class="label label-warning">现金</span></td>';
					break;
					case 3:
						temp+='<td><span class="label label-primary">银行卡</span></td>';
					break;
					default:
						temp+='<td>-</td>';
					break;
				}
				temp+='<td>'+(parseInt(list[i].userid) ? userlist[list[i].userid] : "后台")+'</td>';
				temp+=parseInt(list[i].usetype)==1 ? '<td class="green"> '+fee2num(list[i].cash)+'</td>':'<td class="orange"> +'+fee2num(list[i].cash)+'</td>';
				temp+='<td>'+fee2num(list[i].gift)+'</td><td>'+fee2num(list[i].endfee)+'</td><td>'+fromatTime(list[i].createtime)+'</td>';
				if(parseInt(list[i].status)){
					temp+='<td><span class="label label-success">成功</span></td>';
				}else{
					temp+='<td></td>';
				}
				
				temp+='<td>'+list[i].log+'</td></tr>';
				$("#salelist").append(temp);
			}
			
		}
	});
}

</script>


{/if}